home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 26 / macformat_26.iso / Shareware / Programación / C Reference Card / C Reference Card.rsrc / TEXT_410_10.txt < prev    next >
Text File  |  1997-01-29  |  2KB  |  109 lines

  1. EXCEPTIONS : basics, sample usage
  2. _________________________________________________________________________
  3.  
  4.  
  5. BASICS OF EXCEPTION HANDLING
  6.  
  7. C++ provides means for exception (or error) handling.  You use the 'try', 'catch', and 'throw' keywords for this purpose.  When programming, you tell the compiler to 'try' a section of code.  If an error occurs in the section (or in any nested methods/functions in the section), you 'throw' an exception.  The exception is then handled like a hot potato and is passed up the ladder until it is caught.  If an exception is not caught, the program terminates.  Here's the basic syntax:
  8.  
  9.   // exceptions.cp
  10.  
  11.   #include <iostream.h>
  12.  
  13.   void doFunction( float x );
  14.  
  15.   void main( void )
  16.   {
  17.     float val = 5.0;
  18.   
  19.     try
  20.     {  
  21.       doFunction( val );
  22.     }
  23.     catch( const char *s )
  24.     {
  25.       cout << "string exception caught" << endl;
  26.       cout << s << endl;
  27.     }
  28.     catch(...)
  29.     {
  30.       // catch all other exceptions here
  31.     }
  32.   }
  33.   
  34.   
  35.    void doFunction( float x )
  36.   {
  37.     float limit = 3.14159;
  38.  
  39.     if( x > limit )
  40.       throw "value exceeds limit";
  41.   }
  42.   // end exceptions.cp
  43.  
  44. In most C++ applications, you 'throw' an exception class object rather than a string or error code.  An example of this is shown below:
  45.  
  46.   // potatoe.cp
  47.  
  48.   #include <iostream.h>
  49.  
  50.   void doFunction( float x );
  51.  
  52.   class HotPotatoe
  53.   {  
  54.     public:
  55.       HotPotatoe(float m, float v) {max=m; val=v;}
  56.       float max;
  57.       float val;
  58.   };
  59.   
  60.  
  61.   void main( void )
  62.   {
  63.     float val = 5.0;
  64.   
  65.     try
  66.     {  
  67.       cout << "Example One:" << endl;
  68.       doFunction( val );
  69.     }
  70.     catch( HotPotatoe &thePotatoe )
  71.     {
  72.       cout << "exception caught" << endl;
  73.       cout << "  max. limit: " << thePotatoe.max << endl;
  74.       cout << "  value: " << thePotatoe.val << endl << endl;
  75.     }
  76.     
  77.     try
  78.     {
  79.       cout << "Example Two:" << endl;
  80.       try
  81.       {
  82.         doFunction( val );
  83.       }
  84.       catch( HotPotatoe )
  85.       {
  86.         cout << "exception caught" << endl;
  87.         cout << "exception not handled" << endl;
  88.         cout << "rethrow exception" << endl;
  89.         throw;
  90.       }
  91.     }
  92.     catch(...)
  93.     {
  94.       cout << "rethrown exception caught" << endl;
  95.     }
  96.   }
  97.   
  98.   
  99.   void doFunction( float x )
  100.   {
  101.     float limit = 3.14159;
  102.  
  103.     if( x > limit )
  104.     {
  105.       cout << "throw exception" << endl;
  106.       throw HotPotatoe(limit,x);
  107.     }
  108.   }
  109.   // end potatoe.cp